allow integer indexes as strings

Andrew Cantino 9 years ago
parent
commit
3584c8fa5e

+ 6 - 2
app/models/agents/website_agent.rb

@@ -231,7 +231,7 @@ module Agents
231 231
               when Integer
232 232
                 # ok
233 233
               when String
234
-                if re && !re.names.include?(index)
234
+                if index.to_i.to_s != index && re && !re.names.include?(index)
235 235
                   errors.add(:base, "no named capture #{index.inspect} found in regexp for #{name.inspect})")
236 236
                 end
237 237
               when nil
@@ -429,7 +429,11 @@ module Agents
429 429
         regexp = Regexp.new(extraction_details['regexp'])
430 430
         result = []
431 431
         doc.scan(regexp) {
432
-          result << Regexp.last_match[extraction_details['index']]
432
+          index_or_named_group = extraction_details['index']
433
+          if index_or_named_group.is_a?(String) && index_or_named_group.to_i.to_s == index_or_named_group
434
+            index_or_named_group = index_or_named_group.to_i
435
+          end
436
+          result << Regexp.last_match[index_or_named_group]
433 437
         }
434 438
         log "Extracting #{extraction_type} at #{regexp}: #{result}"
435 439
         result

+ 2 - 0
spec/concerns/liquid_interpolatable_spec.rb

@@ -105,6 +105,8 @@ describe LiquidInterpolatable::Filters do
105 105
         to_return(status: 301, headers: { Location: 'http://tinyurl.com.x/cccc' })
106 106
       stub_request(:head, 'http://tinyurl.com.x/cccc').
107 107
         to_return(status: 301, headers: { Location: 'http://www.example.com/welcome' })
108
+      stub_request(:head, 'http://www.example.com/welcome').
109
+        to_return(status: 200)
108 110
 
109 111
       (1..5).each do |i|
110 112
         stub_request(:head, "http://2many.x/#{i}").

+ 3 - 3
spec/models/agents/website_agent_spec.rb

@@ -577,7 +577,7 @@ fire: hot
577 577
             'mode' => 'on_change',
578 578
             'extract' => {
579 579
               'word' => { 'regexp' => '^(.+?): (.+)$', index: 1 },
580
-              'property' => { 'regexp' => '^(.+?): (.+)$', index: 2 },
580
+              'property' => { 'regexp' => '^(.+?): (.+)$', index: '2' },
581 581
             }
582 582
           }
583 583
           @checker = Agents::WebsiteAgent.new(name: 'Text Site', options: site)
@@ -585,7 +585,7 @@ fire: hot
585 585
           @checker.save!
586 586
         end
587 587
 
588
-        it "works with regexp" do
588
+        it "works with regexp with named capture" do
589 589
           @checker.options = @checker.options.merge('extract' => {
590 590
             'word' => { 'regexp' => '^(?<word>.+?): (?<property>.+)$', index: 'word' },
591 591
             'property' => { 'regexp' => '^(?<word>.+?): (?<property>.+)$', index: 'property' },
@@ -602,7 +602,7 @@ fire: hot
602 602
           expect(event2.payload['property']).to eq('hot')
603 603
         end
604 604
 
605
-        it "works with regexp with named capture" do
605
+        it "works with regexp" do
606 606
           expect {
607 607
             @checker.check
608 608
           }.to change { Event.count }.by(2)